package com.ityunhe.biz;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import com.ityunhe.bean.Comment;
import com.ityunhe.bean.Garden;
import com.ityunhe.bean.Goods;
import com.ityunhe.bean.Page;
import com.ityunhe.dao.GardenPageManageDao;
import com.ityunhe.dao.impl.GardenPageManageDaoImpl;

public class GardenPageManageBis {
	/**
	 * 菜园主页轮播图
	 * 获取收藏量前四名的菜园id与第一张图片地址
	 * 
	 * @return List<Garden>
	 */
	public List<Garden> carouselShow(){
		//对菜园收藏表按菜园分组，统计收藏的用户数量，取得前4名gaid,然后用去菜园表中查询，得到 菜园id和菜园图片字段值
		//对应的sql语句是select gaid,gardenimages from garden where gaid in
		//(select gaid from (select gaid,count(uid) as cum from collectgarden group by gaid order by cum desc limit 0,4) a);
		GardenPageManageDao gardenPageManageDaoImpl =new GardenPageManageDaoImpl();
		List<Garden> gardenList = gardenPageManageDaoImpl.carouselShow();
		return gardenList;
	}
	/**
	 * 
	 * 收藏量最大的4个商品
	 */
	public List<Goods> recommendGoodsList(){
		GardenPageManageDao gardenPageManageDaoImpl =new GardenPageManageDaoImpl();
		List<Goods> recommendGoodsList = gardenPageManageDaoImpl.recommendGoodsList();
		for (int i = 0; i < recommendGoodsList.size(); i++) {
			Goods goods=recommendGoodsList.get(i);
			if(goods.getGoodsimages().indexOf("*")!=-1){
				goods.setGoodsimages(goods.getGoodsimages().substring(0, goods.getGoodsimages().indexOf("*")));
			}else if(goods.getGoodsimages().matches("^[.]+.jsp$")){
				recommendGoodsList.remove(goods);
			}
		}
		return recommendGoodsList;	 
	}
	/**
	 * 
	 *获取按二级商品某个分类收藏前12名
	 *select b.*,count(1) as num from collectgoods a,goods b where a.goid=concat(b.gsid,',',b.gaid,',',b.goid) and b.gsid in (select gsid from goodsclasses where parentid=(select gsid from goodsclasses where classesname='蔬菜')) group by goid order by num desc limit 0,12;
	 */
	public List<Goods> recommendGoodsClassesList(String classesName){
		GardenPageManageDao gardenPageManageDaoImpl =new GardenPageManageDaoImpl();
		List<Goods> recommendGoodsClassesList = gardenPageManageDaoImpl.recommendGoodsClassesList(classesName);
		for (int i = 0; i < recommendGoodsClassesList.size(); i++) {
			Goods goods=recommendGoodsClassesList.get(i);
			if(goods.getGoodsimages().indexOf("*")!=-1){
				goods.setGoodsimages(goods.getGoodsimages().substring(0, goods.getGoodsimages().indexOf("*")));
			}else if(goods.getGoodsimages().matches("^[.]+.jsp$")){
				recommendGoodsClassesList.remove(goods);
			}
		}
		return recommendGoodsClassesList;
	}
	public Map<String,Object> showGardenItemPage(Integer gaid) {		
		//拿到菜园信息
		GardenPageManageDao gardenPageManageDaoImpl =new GardenPageManageDaoImpl();
		Garden garden = gardenPageManageDaoImpl.getGardenById(gaid);
		Map<String,Object> map=new HashMap<String,Object>();
		//拿到库存前24商品信息
		List<Goods> goodsList = gardenPageManageDaoImpl.getGoodsListByGardenId(gaid);
		map.put("garden", garden);
		for (int i = 0; i < goodsList.size(); i++) {
			Goods goods=goodsList.get(i);
			if(goods.getGoodsimages().indexOf("*")!=-1){
				goods.setGoodsimages(goods.getGoodsimages().substring(0, goods.getGoodsimages().indexOf("*")));
			}else if(goods.getGoodsimages().matches("^[.]+.jsp$")){
				goodsList.remove(goods);
			}
		}
		map.put("goodsList", goodsList);
		return map;
	}
	//获取详情页中的所有信息
	public Map<String,Object> showIntroductionPage(Integer goid){
		GardenPageManageDao gardenPageManageDaoImpl =new GardenPageManageDaoImpl();
		Map<String,Object> map=new HashMap<String,Object>();
		//拿到商品信息
		Goods goods = gardenPageManageDaoImpl.getGoodsById(goid);
		String[] split = goods.getGoodsimages().split("[*]");
		List<String> imagesList=Arrays.asList(split);
		map.put("imagesList", imagesList);
		map.put("goods", goods);
		//根据商品信息中的菜园id拿到菜园信息
		Garden garden = gardenPageManageDaoImpl.getGardenById(goods.getGaid());
		map.put("garden", garden);
		//获取商品评价第一分页信息
		Page<Comment> page=new Page<Comment>();
		page.setPageSize(30);
		page.setPageIndex(1);
		page = getCommentPage(page,goid);
		map.put("page", page);
		return map;
	}
	//获取详情页中的评价
	public Page<Comment> getCommentPage(Page<Comment> page,Integer goid){
		GardenPageManageDao gardenPageManageDaoImpl =new GardenPageManageDaoImpl();
		HashMap<String,Object> hashMap = new HashMap<String,Object>();
		hashMap.put("startIndex", page.getPageIndex()*page.getPageSize());
		hashMap.put("pageSize", page.getPageSize());
		//获取商品评价表中的商品id并存放到hashmap中
		Goods goods = gardenPageManageDaoImpl.getGoodsById(goid);
		hashMap.put("goid",goods.getGsid()+","+goods.getGaid()+","+goods.getGoid());
		//根据评价表中的商品id获取总记录数
		int commentCount = gardenPageManageDaoImpl.getCommentCountByGoid(goods.getGsid()+","+goods.getGaid()+","+goods.getGoid());
		page.setTotleCount(commentCount);
		//根据hashmap信息得到分页commentlist结果
		List<Comment> commentList = gardenPageManageDaoImpl.getCommentByHashMap(hashMap);
		page.setList(commentList);	
		return page;
	}
	//showIntroductionPage(Integer goid)测试
	@Test
	public void fun(){
		Map<String, Object> showIntroductionPage = showIntroductionPage(1);
		Goods goods = (Goods) showIntroductionPage.get("goods");
		System.out.println(goods);
		Garden garden=(Garden) showIntroductionPage.get("garden");
		System.out.println(garden);
		List<String> imagesList=(List<String>) showIntroductionPage.get("imagesList");
		for (String string : imagesList) {
			System.out.println(string);
		}
		Page<Comment> page=(Page<Comment>) showIntroductionPage.get("page");
		System.out.println(page);	
	}
}
